// customerPackage/pages/customerDetail/customerDetail.js

const app = getApp()


import {
  pycode
} from '../../../utils/pinyin'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    path: app.globalData.path,
    customer: '',
    editFlag: false, //编辑模式
    userFlag: false, //  true  客户信息   false 客户数据统计
    navFlag: 1, //1 销售记录  2 拜访记录
    salesSum: 100, //门店总计销售额
    salesCount: 0, //销量


    outStocks: [], // 门店销售记录
    visitList: [], //拜访记录
  },

  // 切换顶部导航栏
  changeTab(e) {
    var flag = e.currentTarget.dataset.flag;
    this.setData({
      userFlag: flag == 'info'
    })
  },
  // 切换销售记录  拜访记录 导航
  selectNav(e) {
    console.log(e);
    var flag = e.currentTarget.dataset.flag;
    console.log(flag);
    this.setData({
      navFlag: flag
    })

  },

  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },

  infoHandle(e) {
    console.log(this.data.editFlag);

    if (!this.data.editFlag) {
      // 当前未编辑   变成编辑状态
      this.setData({
        editFlag: true
      })
    } else {
      // 保存用户信息

      var customer = this.data.customer;

      if (customer.customerShopName == '') {
        wx.showToast({
          title: '请输入门店名称',
          icon: 'none'
        })
        return
      }
      if (customer.customerName == '') {
        wx.showToast({
          title: '请输入客户名称',
          icon: 'none'
        })
        return
      }
      if (customer.customerTel == '') {
        wx.showToast({
          title: '请输入联系方式',
          icon: 'none'
        })
        return
      }
      if (customer.customerSex == '') {
        wx.showToast({
          title: '请输入性别',
          icon: 'none'
        })
        return
      }



      var url = app.globalData.url + "customer/addCustomer",
        that = this,
        firstWord = this.getF(that.data.customer.customerShopName);
      console.log(firstWord);

      console.log(customer);

      wx.showLoading({
        title: '',
      })
      app.wxRequest(url, {
          userId: app.globalData.userId,
          companyId: app.globalData.companyId,
          customerId: that.data.customer.customerId,
          customerSex: that.data.customer.customerSex,
          customerName: that.data.customer.customerName,
          customerShopName: that.data.customer.customerShopName,
          customerTel: that.data.customer.customerTel,
          customerAddress: that.data.customer.customerAddress,
          customerNote: that.data.customer.customerNote,
          customerLong: that.data.customer.customerLong,
          customerLat: that.data.customer.customerLat,
          customerPosition: that.data.customer.customerPosition,
          customerFirstWord: firstWord,
        }, res => {
          console.log(res);
          wx.hideLoading()
          if (res.flag) {
            var res = res.data
            wx.showModal({
              title: '提示',
              content: '信息修改成功',
              showCancel: false,
              complete: (res) => {
                if (res.confirm) {
                  this.setData({
                    editFlag: false
                  })
                }
              }
            })
          } else {
            wx.showToast({
              title: '保存失败',
              icon: 'none'
            })
          }
        },
        err => {
          console.log(err);
        })
    }
  },
  callPhone(e) {
    var tel = e.currentTarget.dataset.tel;
    wx.makePhoneCall({
      phoneNumber: tel,
    })
  },


  getCusOrder() {
    var url = app.globalData.url + "outStock/listCustomerOutStock",
      that = this,
      salesSum = 0, //门店总计销售额
      salesCount = 0 //销量;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        customerId: that.data.customer.customerId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var result = res.data
          wx.hideLoading()
          console.log(res);
          if (result.length > 0) {
            result.forEach(item => {
              salesCount += item.productNum
              salesSum += item.totalMoney * 1
            })

          }

          that.setData({
            outStocks: result,
            salesCount,
            salesSum
          })
        }
      },
      err => {
        console.log(err);
      })

  },

  getCusVisit() {
    var url = app.globalData.url + "customer/listCustomerVisit",
      that = this;
    wx.showLoading({
      title: '',
    })
    app.wxRequest(url, {
        customerId: that.data.customer.customerId,
      }, res => {
        console.log(res);
        if (res.flag) {
          var result = res.data
          wx.hideLoading()
          console.log(res);
          that.setData({
            visitList: result,
          })

          console.log(that.data.visitList);
        }
      },
      err => {
        console.log(err);
      })

  },


  // 查看订单详情
  toOrderDetail(e) {
    var index = e.currentTarget.dataset.index,
      list = this.data.outStocks;
    console.log(list[index]);

    wx.navigateTo({
      url: '/customerPackage/pages/orderDetail/orderDetail?outStock=' + JSON.stringify(list[index]) + "&customer=" + JSON.stringify(this.data.customer),
    })
  },

  kaiDan() {
    wx.navigateTo({
      url: '/productPackage/pages/outStock/outStock?cusId=' + this.data.customer.customerId,
    })
  },

  visitHandle() {
    console.log('拜访');
    wx.navigateTo({
      url: '/customerPackage/pages/visit/visit?customer=' + JSON.stringify(this.data.customer),
    })
  },

  getF(str) {
    return pycode[str.toUpperCase().substr(0, 1)]
  },

  onChange(event) {
    var str = event.currentTarget.dataset.flag
    this.setData({
      [str]: event.detail
    });
    console.log(this.data);

  },



  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      customer: JSON.parse(options.customer)
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {


    this.getCusOrder()
    this.getCusVisit()

    console.log(this.data);
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})